package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/*这段程序的作用：统计tagurl表中每个网站被抓取了几次*/

public class StatisticsWebTimes {
	public void statisticsWebTimes() { // a;sldfkja;sdjf;asjdf;asjdf
		Connection connSata = null;
		Statement stmt = null;
		ResultSet rs = null;
		Statement stmt2 = null;
		ResultSet rs2 = null;
		Statement stmt3 = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connSata = DriverManager.getConnection("jdbc:mysql://" + host + "/" + dBName + "?user=root");
			stmt = connSata.createStatement();
			rs = stmt.executeQuery("select DISTINCT urlnotail from tagurl");
			while (rs.next()) {
				stmt2 = connSata.createStatement();
				stmt3 = connSata.createStatement();
				rs2 = stmt2.executeQuery("SELECT COUNT(*) FROM tagurl where urlnotail = '" + rs.getString(1) + "'");
				while (rs2.next()) {
					String sql = "insert into urlstat (url,times) values('" + rs.getString(1) + "'," + rs2.getInt("COUNT(*)") + ")";
					stmt3.executeUpdate(sql);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (rs2 != null) {
					rs2.close();
					rs2 = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (stmt2 != null) {
					stmt2.close();
					stmt2 = null;
				}
				if (stmt3 != null) {
					stmt3.close();
					stmt3 = null;
				}
				if (connSata != null) {
					connSata.close();
					connSata = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	String host = null;
	String dBName = null;

	public void setHost(String host) {
		this.host = host;
	}

	public void setdBName(String dBName) {
		this.dBName = dBName;
	}

}
